package controller;

import java.io.File;

import model.OOModel;
import model.OOPackage;


public class DirectoryTraverseTree {

	private OOModel ooModel;
	private Tree ptree = new Tree();
	private boolean flag = true;
	private OOPackage temp= new OOPackage();
	
	public DirectoryTraverseTree(OOModel ooModel) {
		this.ooModel=ooModel;
	}

	public Tree getPtree() {
		return ptree;
	}
	
	// This method is to call traverse then add the packages to OOModel
	public void processDirectory(File dir)
	{
		traverse(dir);
		ooModel.packages.add(ptree.getRootElement());
	}
	
	// Process only directories under dir
	private void traverse(File dir)
	{
		OOPackage tempIn= new OOPackage();
		OOPackage p=new OOPackage();
		p.name=dir.getName();
		p.path=dir.getPath();
		p.parent=dir.getParentFile();
		if(flag)
		{
			ptree.setRootElement(p);
			temp = ptree.getRootElement();
			flag=false;
		}
		else
			temp.addChild(p);

		File[] files = dir.listFiles();
		for (File file : files)
		{
			if (file.isDirectory())
			{
				temp=p;
				tempIn=temp;
				traverse(file);
				temp=tempIn;
			}
		}
	}
}